using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace IcyTowerMobile
{
	/// <summary>
	/// Class representing a button in a message box.
	/// </summary>
	public class MessageBoxButton
	{

		private Texture2D texture;
		private Texture2D pressedTexture;
		private Vector2 position;
		private String text;
		private Rectangle boundingBox;
		private bool pressed;

		public Rectangle BoundingBox
		{
			get { return boundingBox; }
		}

		public int Height
		{
			get { return pressed ? pressedTexture.Height : texture.Height; }
		}

		public int Width
		{
			get { return pressed ? pressedTexture.Width : texture.Width; }
		}


		/// <summary>
		/// Initializes the message box button.
		/// </summary>
		/// <param name="texture">Button texture.</param>
		/// <param name="pressedTexture">Button texture in pressed state.</param>
		/// <param name="xPos">X position of the button.</param>
		/// <param name="yPos">Y position of the button.</param>
		/// <param name="text">Caption of the button.</param>
		public void Initialize(Texture2D texture, Texture2D pressedTexture, int xPos, int yPos, String text)
		{
			this.texture = texture;
			this.pressedTexture = pressedTexture;
			position = new Vector2(xPos, yPos);
			boundingBox = new Rectangle(xPos, yPos, texture.Width, texture.Height);
			this.text = text;
		}

		/// <summary>
		/// Allows the game component to update itself.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		public void Update(GameTime gameTime, Vector2 pressPosition)
		{
			if (Contains(pressPosition))
			{
				pressed = true;
			}
			else
			{
				pressed = false;
			}
		}

		/// <summary>
		/// Checks if the given position is in the button's bounding box.
		/// </summary>
		/// <param name="position">Position to check.</param>
		/// <returns>True if the position is in button area. False otherwise.</returns>
		public bool Contains(Vector2 position)
		{
			return boundingBox.Contains((int)position.X, (int)position.Y);
		}

		public void Draw(SpriteBatch spriteBatch, SpriteFont font, Color color)
		{
			if (pressed)
			{
				// draw pressed texture
				spriteBatch.Draw(pressedTexture, position, color);
			}
			else
			{
				// draw normal texture
				spriteBatch.Draw(texture, position, color);
			}

			// draw button caption
			Vector2 textSize = font.MeasureString(text);
			Vector2 textPosition = new Vector2(position.X + (Width - textSize.X) / 2, position.Y + (Height - textSize.Y) / 2);
			spriteBatch.DrawString(font, text, textPosition, Color.Black);
		}
	}
}
